<?php

Yii::import('application.models._base.BaseShare');

class Share extends BaseShare
{
	public static function model($className=__CLASS__) {
		return parent::model($className);
	}
    public function rules() {
        return array(
            array('iid, poster_id, poster_nickname, original_id, uid, u_nickname, create_time, album_id, category_id', 'required','on'=>'create'),
            array('iid, poster_id, original_id, uid, total_comments, total_click, total_like, total_forwarding, create_time, album_id, category_id', 'numerical', 'integerOnly'=>true),
            array('poster_nickname, u_nickname', 'length', 'max'=>20),
            array('from,is_delete, total_comments, total_click, total_like, total_forwarding', 'safe'),
            array('id,is_delete,from, iid, poster_id, poster_nickname, original_id, uid, u_nickname, total_comments, total_click, total_like, total_forwarding, create_time, album_id, category_id', 'safe', 'on'=>'search'),
        );
    }

    public function getSharePk($id){
        return $this->findByPk($id);
    }


    public function editShare($id,$albumid,$categoryid,$from){
        $info = $this->findByPk($id);
        $info->album_id = $albumid;
        $info->category_id = $categoryid;
        $info->from = $from;
        if ($info->save()) {
            $data['status'] = 1;
            $data['message'] = '收藏修改成功';
        }else{
            $data['status'] = 0;
            $data['message'] = $this->showError($info->getErrors());
        }
        return $data;
    }

    /**
     * imageid , albumid  categoryid  uid nick_name
     * 原发型share
     */
    public function createShare($imageid,$albumid,$categoryid,$uid,$nick_name){
        $Share_db = new Share();
        $Share_db->setScenario('create');
        $Share_data = array(
            'iid'=>$imageid,
            'poster_id'=>$uid,
            'poster_nickname'=>$nick_name,
            'original_id'=>0, //原始ID为0  原创
            'uid'=>$uid,
            'u_nickname'=>$nick_name,
            'total_comments'=>0,
            'total_click'=>0,
            'total_like'=>0,
            'total_forwarding'=>0,
            'create_time'=>time(),
            'album_id'=>$albumid,
            'category_id'=>$categoryid
        );
        $Share_db->setAttributes($Share_data);
        if ($Share_db->save()) {
            $id = $Share_db->attributes['id'];
            //发布总数统计写入User表
            $this->userShareCount($uid);
            $data['status'] = 1;
            $data['id'] = $id;
        }else{
            $data['status'] = 0;
            $data['message'] = $this->showError($Share_db->getErrors());
        }
        return $data;
    }
    public function userShareCount($uid){
        $user_info = User::model()->findByPk($uid);
        $share_count = $this->count('uid=:uid and original_id=:original and is_delete=:is_delete',array(':uid'=>$uid,':original'=>0,':is_delete'=>0));
        $user_info->total_shares = $share_count;
        $user_info->update();
    }
    public function userShareStorageCount($uid){
        $user_info = User::model()->findByPk($uid);
        $share_count = $this->count('uid=:uid and original_id !=:original and is_delete=:is_delete',array(':uid'=>$uid,':original'=>0,':is_delete'=>0));
        $user_info->total_forward = $share_count;
        $user_info->update();
    }

    public function getAllShareAlbum($share_id,$first,$total){
        $db = Yii::app()->db->createCommand();
        $info = $db
            ->select('a.title,a.id,a.album_cover,a.total_share,a.total_like')
            ->from('{{share}} s')
            ->join('{{album}} a','s.album_id=a.id')
            ->where('s.original_id=:original_id and a.is_delete=:ais_delete and s.is_delete=:sis_delete',array(':original_id'=>$share_id,':ais_delete'=>0,':sis_delete'=>0))
            ->order('s.create_time desc')
            ->limit($total,$first)
            ->queryAll();
        return $info;
    }
    /**
     * 取得图片列表
     * @param $aid
     * @param $uid
     * @param $first 第几次  例如 0 10 20 30 40
     * @param $page  大分页
     * @param $total 取几条   例如10
     * @return mixed
     */
    public function getShare($aid,$uid,$uuid,$first,$total){
        $db = Yii::app()->db->createCommand();
        $info = $db
            ->select('s.from,s.category_id,s.uid,fs.share_id as fs,sc.comment,s.id,s.iid,i.content,i.is_delete,i.image_path,s.total_comments,s.total_like,s.total_forwarding')
            ->from('{{share}} s')
            ->leftJoin('{{item}} i','s.iid=i.id')
            ->leftJoin('{{share_comment}} sc','sc.id=s.id')
            ->leftJoin('{{favorite_share}} fs','fs.share_id=s.id AND fs.user_id=:uuid',array(':uuid'=>$uuid))
            ->where('s.uid=:uid and s.album_id=:aid and i.is_show=:is_show and i.is_delete=:is_delete and s.is_delete=:sis_delete',array(':uid'=>$uid,':aid'=>$aid,':is_show'=>1,':is_delete'=>0,':sis_delete'=>0))
            ->order('s.create_time desc')
            ->limit($total,$first)
            ->queryAll();
        return $info;
    }

    public function getThreeShare($uid){
        $db = Yii::app()->db->createCommand();
        $info = $db
            ->select('s.id,i.image_path')
            ->from('{{share}} s')
            ->leftJoin('{{item}} i','s.iid=i.id')
            ->where('s.uid=:uid and i.is_show=:is_show and i.is_delete=:is_delete and s.is_delete=:sis_delete',array(':uid'=>$uid,':is_show'=>1,':is_delete'=>0,':sis_delete'=>0))
            ->order('s.create_time desc')
            ->limit(3)
            ->queryAll();
        return $info;
    }

    public function getUserFavoriteShareCount($uid){
        $db = Yii::app()->db->createCommand();
        $info = $db->select('count(*) as count')
            ->from('{{favorite_share}} fs')
            ->leftJoin('{{share}} s','fs.share_id=s.id')
            ->leftJoin('{{item}} i','s.iid=i.id')
            ->where('fs.user_id=:uid and i.is_show=:is_show and i.is_delete=:is_delete and s.is_delete=:sis_delete',array(':uid'=>$uid,':is_show'=>1,':is_delete'=>0,':sis_delete'=>0))
            ->order('fs.create_time desc')
            ->queryRow();
        return $info['count'];
    }
    public function getUserForwardShareCount($uid){
        $db = Yii::app()->db->createCommand();
        $info = $db->select('count(*) as count')
            ->from('{{share}} s')
            ->leftJoin('{{item}} i','s.iid=i.id')
            ->where('s.uid=:uid and s.original_id !=:original_id and i.is_show=:is_show and i.is_delete=:is_delete and s.is_delete=:sis_delete',array(':uid'=>$uid,':original_id'=>0,':is_show'=>1,':is_delete'=>0,':sis_delete'=>0))
            ->order('s.create_time desc')
            ->queryRow();
        return $info['count'];
    }
    public function getUserSharesCount($uid){
        $db = Yii::app()->db->createCommand();
        $info = $db->select('count(*) as count')
            ->from('{{share}} s')
            ->leftJoin('{{item}} i','s.iid=i.id')
            ->where('s.uid=:uid and s.original_id =:original_id and i.is_show=:is_show and i.is_delete=:is_delete and s.is_delete=:sis_delete',array(':uid'=>$uid,':original_id'=>0,':is_show'=>1,':is_delete'=>0,':sis_delete'=>0))
            ->order('s.create_time desc')
            ->queryRow();
        return $info['count'];
    }

    public function getUserFavoriteShare($uid,$uuid,$first,$total){
        $db = Yii::app()->db->createCommand();
        $info = $db->select('s.from,s.category_id,s.uid,fs1.share_id as fs,sc.comment,s.id,s.iid,i.content,i.is_delete,i.image_path,s.total_comments,s.total_like,s.total_forwarding')
            ->from('{{favorite_share}} fs')
            ->leftJoin('{{share}} s','fs.share_id=s.id')
            ->leftJoin('{{item}} i','s.iid=i.id')
            ->leftJoin('{{share_comment}} sc','sc.id=s.id')
            ->leftJoin('{{favorite_share}} fs1','fs1.share_id=s.id AND fs1.user_id=:uuid',array(':uuid'=>$uuid))
            ->where('fs.user_id=:uid and i.is_show=:is_show and i.is_delete=:is_delete and s.is_delete=:sis_delete',array(':uid'=>$uid,':is_show'=>1,':is_delete'=>0,':sis_delete'=>0))
            ->order('fs.create_time desc')
            ->limit($total,$first)
            ->queryAll();
        return $info;
    }
    public function getUserForwardShare($uid,$uuid,$first,$total){
        $db = Yii::app()->db->createCommand();
        $info = $db->select('s.from,s.category_id,s.uid,fs.share_id as fs,sc.comment,s.id,s.iid,i.content,i.is_delete,i.image_path,s.total_comments,s.total_like,s.total_forwarding')
            ->from('{{share}} s')
            ->leftJoin('{{item}} i','s.iid=i.id')
            ->leftJoin('{{share_comment}} sc','sc.id=s.id')
            ->leftJoin('{{favorite_share}} fs','fs.share_id=s.id AND fs.user_id=:uuid',array(':uuid'=>$uuid))
            ->where('s.uid=:uid and s.original_id !=:original_id and i.is_show=:is_show and i.is_delete=:is_delete and s.is_delete=:sis_delete',array(':uid'=>$uid,':original_id'=>0,':is_show'=>1,':is_delete'=>0,':sis_delete'=>0))
            ->order('fs.create_time desc')
            ->limit($total,$first)
            ->queryAll();
        return $info;
    }
    public function getUserShares($uid,$uuid,$first,$total){
        $db = Yii::app()->db->createCommand();
        $info = $db->select('s.from,s.category_id,s.uid,fs.share_id as fs,sc.comment,s.id,s.iid,i.content,i.is_delete,i.image_path,s.total_comments,s.total_like,s.total_forwarding')
            ->from('{{share}} s')
            ->leftJoin('{{item}} i','s.iid=i.id')
            ->leftJoin('{{share_comment}} sc','sc.id=s.id')
            ->leftJoin('{{favorite_share}} fs','fs.share_id=s.id AND fs.user_id=:uuid',array(':uuid'=>$uuid))
            ->where('s.uid=:uid and s.original_id =:original_id and i.is_show=:is_show and i.is_delete=:is_delete and s.is_delete=:sis_delete',array(':uid'=>$uid,':original_id'=>0,':is_show'=>1,':is_delete'=>0,':sis_delete'=>0))
            ->order('fs.create_time desc')
            ->limit($total,$first)
            ->queryAll();
        return $info;
    }
    /**
     * 通过category_id  获取此分类里面的share信息
     * @param $category_id
     * @param $first
     * @param $total
     * @return mixed
     */
    public function getCategoryShare($category_id,$uuid,$first,$total,$very=0,$keyword=''){
        $db = Yii::app()->db->createCommand();
        $db->select('s.from,s.category_id,s.uid,fs.share_id as fs,sc.comment,s.id,s.iid,i.content,i.is_delete,i.image_path,s.total_comments,s.total_like,s.total_forwarding');
        $db->from('{{share}} s');
        $db->leftJoin('{{item}} i','s.iid=i.id');
        $db->leftJoin('{{share_comment}} sc','sc.id=s.id');
        $db->leftJoin('{{favorite_share}} fs','fs.share_id=s.id AND fs.user_id=:uuid',array(':uuid'=>$uuid));
        $db->andWhere('i.is_show=:is_show AND i.is_delete=:is_delete AND s.is_delete=:sis_delete',array(':is_show'=>1,':is_delete'=>0,':sis_delete'=>0));
        if($category_id){
            $db->andWhere('s.category_id=:cate_id',array(':cate_id'=>$category_id));
        }
        if($keyword){
            $db->andWhere(array('like', 'i.content', '%'.$keyword.'%'));
        }

        if($very){
            if($very==1){
                $db->order('s.total_like desc,s.create_time desc');
            }elseif($very==2){
                $db->order('s.total_forwarding desc,s.create_time desc');
            }elseif($very==3){
                $db->order('s.total_comments desc,s.create_time desc');
            }else{
                $db->order('s.create_time desc');
            }
        }else{
            $db->order('s.create_time desc');
        }
        $db->limit($total,$first);
        $info = $db->queryAll();
        return $info;
    }

    public function getIndexInfo($id){
        $db = Yii::app()->db->createCommand();
        $info = $db
            ->select('c.title_cn,c.title_en,s.category_id,s.create_time,s.total_forwarding,s.total_like,s.total_comments,s.original_id,s.poster_id,s.id,s.uid,s.iid,s.album_id,i.content,i.image_path,u.nick_name,u.domain')
            ->from('{{share}} s')
            ->leftJoin('{{item}} i','s.iid=i.id')
            ->leftJoin('{{user}} u','u.id=s.uid')
            ->leftJoin('{{category}} c','s.category_id=c.id')
            ->where('s.id=:id and i.is_show=:is_show and i.is_delete=:is_delete AND s.is_delete=:sis_delete',array(':id'=>$id,':is_show'=>1,':is_delete'=>0,':sis_delete'=>0))
            ->queryRow();
        return $info;
    }

    //内页通过id获取  share  item  user信息
    public function getShareInfo($id){
        $db = Yii::app()->db->createCommand();
        $info = $db
            ->select('s.category_id,fs.share_id as fs,s.create_time,sc.comment,s.total_forwarding,s.total_like,s.total_comments,s.original_id,s.poster_id,s.poster_nickname,s.id,s.uid,s.iid,s.album_id,i.content,i.image_path,i.tags,s.from,u.nick_name,u.domain')
            ->from('{{share}} s')
            ->leftJoin('{{item}} i','s.iid=i.id')
            ->leftJoin('{{user}} u','u.id=s.uid')
            ->leftJoin('{{share_comment}} sc','sc.id=s.id')
            ->leftJoin('{{favorite_share}} fs','fs.share_id=s.id')
            ->where('s.id=:id and i.is_show=:is_show and i.is_delete=:is_delete AND s.is_delete=:sis_delete',array(':id'=>$id,':is_show'=>1,':is_delete'=>0,':sis_delete'=>0))
            ->queryRow();
        return $info;
    }
    public function getBasicOneShare($sid){
        $db = Yii::app()->db->createCommand();
        $info = $db
            ->select('s.id,s.iid,i.image_path')
            ->from('{{share}} s')
            ->leftJoin('{{item}} i','s.iid=i.id')
            ->where('s.id=:sid',array(':sid'=>$sid))
            ->queryRow();
        return $info;
    }
    public function getBasicShare($aid,$uid,$first,$total){
        $db = Yii::app()->db->createCommand();
        $info = $db
            ->select('s.id,s.iid,i.image_path')
            ->from('{{share}} s')
            ->leftJoin('{{item}} i','s.iid=i.id')
            ->where('s.uid=:uid and s.album_id=:aid and i.is_show=:is_show and i.is_delete=:is_delete AND s.is_delete=:sis_delete',array(':uid'=>$uid,':aid'=>$aid,':is_show'=>1,':is_delete'=>0,':sis_delete'=>0))
            ->order('s.create_time desc')
            ->limit($total,$first)
            ->queryAll();
        return $info;
    }
    public function getOneShare($id,$uid){
        $db = Yii::app()->db->createCommand();
        $info = $db->select('s.category_id,s.id,i.uid,s.iid,s.category_id,s.album_id,i.content,i.image_path,s.from')
            ->from('{{share}} s')
            ->join('{{item}} i','s.iid=i.id')
            ->where('s.uid=:uid and s.id=:id and i.is_show=:is_show and i.is_delete=:is_delete AND s.is_delete=:sis_delete',array(':uid'=>$uid,':id'=>$id,':is_show'=>1,':is_delete'=>0,':sis_delete'=>0))
            ->queryRow();
        return $info;
    }
    public function getShareImage($id,$uid=''){
        $db = Yii::app()->db->createCommand();
        $db->select('i.image_path');
        $db->from('{{share}} s');
            $db->join('{{item}} i','s.iid=i.id');
        if($uid){
            $db->where('s.uid=:uid and s.id=:id and i.is_show=:is_show and i.is_delete=:is_delete AND s.is_delete=:sis_delete',array(':uid'=>$uid,':id'=>$id,':is_show'=>1,':is_delete'=>0,':sis_delete'=>0));
        }else{
            $db->where('s.id=:id and i.is_show=:is_show and i.is_delete=:is_delete AND s.is_delete=:sis_delete',array(':id'=>$id,':is_show'=>1,':is_delete'=>0,':sis_delete'=>0));
        }
        $info = $db->queryRow();
        return $info['image_path'];
    }
    /**
     * 取得专辑图片总数
     * @param $aid
     * @param $uid
     * @param $first 第几次  例如 0 10 20 30 40
     * @param $page  大分页
     * @param $total 取几条   例如10
     * @return mixed
     */
    public function getShareCount($aid,$uid){
        $db = Yii::app()->db->createCommand();
        $info = $db->select('count(*) as count')
            ->from('{{share}} s')
            ->join('{{item}} i','s.iid=i.id')
            ->where('s.uid=:uid and s.album_id=:aid and i.is_show=:is_show and i.is_delete=:is_delete AND s.is_delete=:sis_delete',array(':uid'=>$uid,':aid'=>$aid,':is_show'=>1,':is_delete'=>0,':sis_delete'=>0))
            ->order('s.create_time desc')
            ->queryRow();
        return $info['count'];
    }

    public function getCategoryShareCount($catid){
        $db = Yii::app()->db->createCommand();
        $db->select('count(*) as count');
        $db->from('{{share}} s');
        $db->join('{{item}} i','s.iid=i.id');
        if($catid){
                $db->where('s.category_id=:catid and i.is_show=:is_show and i.is_delete=:is_delete AND s.is_delete=:sis_delete',array(':catid'=>$catid,':is_show'=>1,':is_delete'=>0,':sis_delete'=>0));
        }else{
                $db->where('i.is_show=:is_show and i.is_delete=:is_delete AND s.is_delete=:sis_delete',array(':is_show'=>1,':is_delete'=>0,':sis_delete'=>0));
        }
        $db->order('s.create_time desc');
        $info = $db->queryRow();
        return $info['count'];
    }

    /**
     * 删除数据
     * @param $id  shareid
     * @param $uid userid
     */
    public function deleteShare($id,$uid){
        $db = Yii::app()->db->createCommand();
        $ok = $db->update('{{share}}',array('is_delete'=>1), 'uid=:uid and id=:id', array(':uid'=>$uid,':id'=>$id));
        if($ok){
            $this->userShareCount($uid);
            $data = 1;
        }else{
            $data = 0;
        }
        return $data;
    }
    public function moveShare($id,$uid,$aid){
        $db = Yii::app()->db->createCommand();
        $ok = $db->update('{{share}}', array('album_id'=>intval($aid)) , 'uid=:uid and id=:id', array(':uid'=>$uid,':id'=>$id));
        if($ok){
            $data = 1;
        }else{
            $data = 0;
        }
        return $data;
    }

    //验证这个收藏是否是这个用户的
    public function checkShare($id,$uid){
        $data = $this->findByPk($id);
        if($data->uid == $uid){
            $return = $data;
        }else{
            $return = 0;
        }
        return $return;
    }
    //用户总喜欢数统计  写入用户表
    public function getFavorite($id,$type='share'){
        if($type=='share'){
            $fs_count = FavoriteShare::model()->count('share_id=:share_id',array(':share_id'=>$id));
            $db = $this->findByPk($id);
            $db->total_like = $fs_count;
            $db->update();
        }elseif($type=='album'){
            $fs_count = FavoriteAlbum::model()->count('album_id=:album_id',array(':album_id'=>$id));
            $db = Album::model()->findByPk($id);
            $db->total_like = $fs_count;
            $db->update();
        }

    }

    public function storageShare($user_auth,$share_id,$album_id){
        $Share_db = new Share();
        $Share_db->setScenario('create');
        $share_info = $this->findByPk($share_id);
        //是否存在个分享
        if($share_info){
            //查看专辑
            $album_info = Album::model()->findByPk($album_id);
            if($album_info->uid == $user_auth['id']){
                //查看这张专辑是否收藏这张图片
                $abled = $this->find('iid=:iid and uid=:uid and album_id=:album_id and is_delete=:is_delete',array(':iid'=>$share_info->iid,':uid'=>$user_auth['id'],':album_id'=>$album_id,':is_delete'=>0));
                if(!$abled){
                    $info = array();
                    $info['iid'] = $share_info->iid;
                    $info['poster_id'] = $share_info->poster_id;
                    $info['poster_nickname'] = $share_info->poster_nickname;
                    $info['original_id'] = $share_id;
                    $info['uid'] = $user_auth['id'];
                    if($user_auth['nick_name']){
                        $info['u_nickname'] = $user_auth['nick_name'];
                    }else{
                        $info['u_nickname'] = $user_auth['username'];
                    }
                    $info['total_comments'] = 0;
                    $info['total_click'] = 0;
                    $info['total_forwarding'] = 0;
                    $info['total_like'] = 0;
                    $info['create_time'] = time();
                    $info['album_id'] = $album_id;
                    $info['category_id'] = $share_info->category_id;
                    $info['from'] = $info['u_nickname'];
                    $Share_db->setAttributes($info);
                    if($Share_db->save()){
                        $id = $Share_db->attributes['id'];
                        //父类用户表收藏 +1
                        $count = $this->storageCount($share_id);
                        $share_info->total_forwarding = $count;
                        $share_info->update();
                        //收藏的这个专辑的封面  专辑收集数  改变
                        Album::model()->updateAlbumCount($album_id,$user_auth['id']);
                        //总收藏数统计
                        $this->userShareStorageCount($user_auth['id']);
                        //写入事件表
                        Event::model()->addEvent(
                            'active',
                            'share',
                            $user_auth['id'],
                            $share_info->uid,
                            $id,
                            '分享'
                        );
                        $data['status'] = 1;
                        $data['url'] = Yii::app()->createUrl('share/show',array('id'=>$id));
                        $data['message'] = '成功收藏这张图片！';
                    }else{
                        $data['status'] = 0;
                        $data['message'] = '收藏图片失败！';
                    }
                }else{
                    $data['status'] = 0;
                    $data['message'] = '你在本专辑已经收藏这张图片啦';
                }
            }else{
                $data['status'] = 0;
                $data['message'] = '你的专辑选择错误';
            }
        }else{
            $data['status'] = 0;
            $data['message'] = '我们没有找到这张图片！';
        }
        return $data;
    }

    private function storageCount($sid){
       return $this->count('original_id=:id and is_delete=:is_delete',array(':id'=>$sid,':is_delete'=>0));
    }
    //获取上一条id
    public function getPrevUrl($album_id,$share_id){
        $db = Yii::app()->db->createCommand();
        $info = $db->select('s.id')
            ->from('{{share}} s')
            ->join('{{item}} i','s.iid=i.id')
            ->where('s.id<:id and s.album_id=:aid and i.is_show=:is_show and i.is_delete=:is_delete and s.is_delete=:sis_delete',array(':id'=>$share_id,':aid'=>$album_id,':is_show'=>1,':is_delete'=>0,':sis_delete'=>0))
            ->order('s.id desc')
            ->queryRow();
        return $info['id'];
    }
    //获取下一条id
    public function getNextUrl($album_id,$share_id){
        $db = Yii::app()->db->createCommand();
        $info = $db->select('s.id')
            ->from('{{share}} s')
            ->join('{{item}} i','s.iid=i.id')
            ->where('s.id>:id and s.album_id=:aid and i.is_show=:is_show and i.is_delete=:is_delete and s.is_delete=:sis_delete',array(':id'=>$share_id,':aid'=>$album_id,':is_show'=>1,':is_delete'=>0,':sis_delete'=>0))
            ->order('s.id asc')
            ->queryRow();
        return $info['id'];
    }


    //显示错误
    public function showError($errors){
        $array = array();
        if($errors){
            foreach($errors as $key=>$value){
                foreach($value as $v){
                    $array[] = $v;
                }
            }
            return $array[0];
        }else{
            return true;
        }
    }
    public function getBasicUserShare($uid,$num){
        $db = Yii::app()->db->createCommand();
        $info = $db
            ->select('s.id,s.iid,i.image_path')
            ->from('{{share}} s')
            ->leftJoin('{{item}} i','s.iid=i.id')
            ->where('s.uid=:uid and s.is_delete=:is_delete',array(':uid'=>$uid,':is_delete'=>0,))
            ->order('s.create_time desc')
            ->limit($num)
            ->queryAll();
        return $info;
    }
}